<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" slot="detail" :rules="rules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="所属子系统" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="systemId">
              <a-select
                style="width: 100%"
                class="search-input"
                showSearch
                optionFilterProp="children"
                :filterOption="filterOption"
                v-model="model.systemId"
                :getPopupContainer="node => node.parentNode"
              >
                <a-select-option v-for="(item, index) in systemList" :key="index" :value="item.systemId">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="接口名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入接口名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请求路径" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="path">
              <a-input v-model="model.path" placeholder="请输入请求路径"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="请求方法" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="apiMethod">
              <a-select
                style="width: 100%"
                showSearch
                optionFilterProp="children"
                :autoClearSearchValue="false"
                v-model="model.apiMethod"
                :getPopupContainer="target => target.parentNode"
              >
                <a-select-option :key="1" :value="1">
                  GET
                </a-select-option>
                <a-select-option :key="2" :value="2">
                  POST
                </a-select-option>
                <a-select-option :key="3" :value="3">
                  PUT
                </a-select-option>
                <a-select-option :key="4" :value="4">
                  DELETE
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="是否项目权限校验"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="projPermCheck"
            >
              <a-select
                style="width: 100%"
                showSearch
                optionFilterProp="children"
                :autoClearSearchValue="false"
                v-model="model.projPermCheck"
                :getPopupContainer="target => target.parentNode"
              >
                <a-select-option :key="0" :value="0">
                  否
                </a-select-option>
                <a-select-option :key="1" :value="1">
                  是
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { api } from '../DpTApiService.js'
export default {
  name: 'DpTApiForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,

      url: {
        systemList: '/datapipeline/biz/dpTSystems',
        add: api.add,
        edit: api.add,
        queryById: api.queryById
      },
      systemList: [],
      rules: {
        name: [{ required: true, message: '请输入接口名称!' }],
        systemId: [{ required: true, message: '请选择所属子系统!' }],
        path: [{ required: true, message: '请输入请求方法!' }],
        apiMethod: [{ required: true, message: '请选择所属子系统!' }],
        projPermCheck: [{ required: true, message: '请选择项目权限校验!' }]
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initData()
  },
  methods: {
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    initData() {
      getAction(this.url.systemList, { pageSize: 10000 }).then(res => {
        if (res.success) {
          this.systemList = res.result.rows
        }
      })
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>

